package com.surelution.projects.crm

class StationNameAliasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
	/**
	 * 未测试过
	 */
	def addAlias = {
		def file = params.aliasName
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(file.inputStream, "GBK"))
		reader.eachLine {line->
			println line
			String[] words = line.split()
			StationNameAlias stationNameAlias = new StationNameAlias(nonGasDescription:"${words[0]}",nonGasType:"${words[1]}")
			println "wwwwwwwww"
			noGasType.save(flush:true)
		}
		println NoGasType.list()
		flash.message = "上传成功！"
		redirect(action:"typeCreate")
	}

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [stationNameAliasInstanceList: StationNameAlias.list(params), stationNameAliasInstanceTotal: StationNameAlias.count()]
    }

    def create = {
        def stationNameAliasInstance = new StationNameAlias()
        stationNameAliasInstance.properties = params
        return [stationNameAliasInstance: stationNameAliasInstance]
    }

    def save = {
        def stationNameAliasInstance = new StationNameAlias(params)
        if (stationNameAliasInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), stationNameAliasInstance.id])}"
            redirect(action: "show", id: stationNameAliasInstance.id)
        }
        else {
            render(view: "create", model: [stationNameAliasInstance: stationNameAliasInstance])
        }
    }

    def show = {
        def stationNameAliasInstance = StationNameAlias.get(params.id)
        if (!stationNameAliasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
            redirect(action: "list")
        }
        else {
            [stationNameAliasInstance: stationNameAliasInstance]
        }
    }

    def edit = {
        def stationNameAliasInstance = StationNameAlias.get(params.id)
        if (!stationNameAliasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [stationNameAliasInstance: stationNameAliasInstance]
        }
    }

    def update = {
        def stationNameAliasInstance = StationNameAlias.get(params.id)
        if (stationNameAliasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (stationNameAliasInstance.version > version) {
                    
                    stationNameAliasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'stationNameAlias.label', default: 'StationNameAlias')] as Object[], "Another user has updated this StationNameAlias while you were editing")
                    render(view: "edit", model: [stationNameAliasInstance: stationNameAliasInstance])
                    return
                }
            }
            stationNameAliasInstance.properties = params
            if (!stationNameAliasInstance.hasErrors() && stationNameAliasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), stationNameAliasInstance.id])}"
                redirect(action: "show", id: stationNameAliasInstance.id)
            }
            else {
                render(view: "edit", model: [stationNameAliasInstance: stationNameAliasInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def stationNameAliasInstance = StationNameAlias.get(params.id)
        if (stationNameAliasInstance) {
            try {
                stationNameAliasInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationNameAlias.label', default: 'StationNameAlias'), params.id])}"
            redirect(action: "list")
        }
    }
}
